/**
 * 用户状态存储
 * Copyright (c) 2023 dfec
 */

// 登录，登出的方法 获取用户详情的接口
import { getInfo, login, logout } from '@/http/apis/index'
// token相关的方法
import { getToken, setToken, removeToken } from '@/utils/token'
const user = {
    state: {
        // token
        token: getToken(),
        // 用户名
        name: '',
        // 用户头像
        avatar: '',
        // 用户的角色权限
        roles: [],
        // 用户的操作权限
        permissions: [],
        // 用户的uuid
        uuid: '',
        // 用户信息
        userInfo: {}
    },

    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_NAME: (state, name) => {
            state.name = name
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
        SET_PERMISSIONS: (state, permissions) => {
            state.permissions = permissions
        },
        SET_UUID: (state, uuid) => {
            state.uuid = uuid
        },
        SET_USERINFO: (state, userInfo) => {
            state.userInfo = userInfo
        }
    },

    actions: {
        // 登录
        Login({ commit }, userInfo) {
            return new Promise((resolve, reject) => {
                login(userInfo)
                    .then(res => {
                        setToken(res.data)
                        commit('SET_TOKEN', res.data)
                        resolve()
                    })
                    .catch(error => {
                        reject(error)
                    })
            })
        },

        // 获取用户信息
        GetInfo({ commit }) {
            return new Promise((resolve, reject) => {
                getInfo()
                    .then(res => {
                        // 没有 data 数据，赋予个默认值
                        if (!res) {
                            res = {
                                data: {
                                    roleIds: [],
                                    user: {
                                        id: '',
                                        avatar: '',
                                        userName: '',
                                        nickname: ''
                                    }
                                }
                            }
                        }
                        const user = res.data.user
                        commit('SET_USERINFO', user)
                        const avatar =
                            user.avatar === '' || user.avatar === null
                                ? require('@/assets/images/pic-logo-light.png')
                                : process.env.VUE_APP_BASE_API + user.avatar
                        // 验证返回的roles是否是一个非空数组
                        if (res.data.roleIds && res.data.roleIds.length > 0) {
                            commit('SET_ROLES', res.data.roleIds)
                            commit('SET_PERMISSIONS', res.data.perms)
                        } else {
                            commit('SET_ROLES', ['ROLE_DEFAULT'])
                        }
                        commit('SET_NAME', user.realName)
                        commit('SET_AVATAR', avatar)
                        resolve(res)
                    })
                    .catch(error => {
                        reject(error)
                    })
            })
        },

        // 退出系统
        LogOut: async function({ commit }) {
            return new Promise(resolve => {
                logout()
                    .then(() => {
                        commit('SET_TOKEN', '')
                        commit('SET_ROLES', [])
                        commit('SET_PERMISSIONS', [])
                        removeToken()
                        resolve(true)
                    })
                    .catch(() => {
                        resolve(false)
                    })
            })
        },

        // 前端 登出
        FedLogOut({ commit }) {
            return new Promise(resolve => {
                commit('SET_TOKEN', '')
                removeToken()
                resolve()
            })
        }
    }
}

export default user
